```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis 动态代理与Mapper接口实现机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.7;
            color: #333;
            background-color: #f9f9f9;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            position: relative;
        }
        .highlight::before {
            content: "";
            position: absolute;
            left: -0.5rem;
            top: 0;
            height: 100%;
            width: 4px;
            background-color: #4f46e5;
            border-radius: 2px;
        }
        .prose {
            max-width: 65ch;
        }
        .mermaid {
            background-color: white;
            padding: 1.5rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <div class="hero text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">MyBatis 动态代理与Mapper接口实现机制</h1>
            <p class="text-xl opacity-90 mb-8">探索MyBatis如何通过动态代理技术将SQL映射方法与实际的数据库操作进行关联</p>
            <div class="flex justify-center space-x-4">
                <a href="#content" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-lg hover:bg-opacity-90 transition">开始阅读</a>
                <a href="#flowchart" class="px-6 py-3 bg-indigo-700 bg-opacity-30 text-white font-medium rounded-lg hover:bg-opacity-40 transition">查看流程图</a>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div id="content" class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-16">
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-12">
            <!-- Main Article -->
            <div class="lg:col-span-2">
                <article class="prose prose-lg max-w-none">
                    <h2 class="text-3xl font-bold mb-6 text-gray-800">1. Mapper接口与动态代理的核心关系</h2>
                    <p class="text-gray-700 mb-6">MyBatis使用动态代理技术来将Mapper接口的方法与具体的SQL操作进行绑定。每个Mapper接口通常对应一个Mapper XML文件，XML文件中定义了SQL语句及其执行方式，而接口中的方法则是调用这些SQL语句的抽象。动态代理的核心目标是将方法调用转化为对<code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>的执行，并通过SQL语句和参数进行动态绑定。</p>

                    <h2 class="text-3xl font-bold mb-6 text-gray-800 mt-12">2. SqlSession与Mapper接口的映射</h2>
                    <p class="text-gray-700 mb-6">在MyBatis中，<code class="bg-gray-100 px-2 py-1 rounded">SqlSession</code>是执行SQL操作的主要接口，提供了获取Mapper接口的方法。<code class="bg-gray-100 px-2 py-1 rounded">SqlSession</code>通过动态代理为Mapper接口创建代理对象，将接口方法调用转发到对应的SQL操作。</p>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public interface SqlSession {
    &lt;T&gt; T getMapper(Class&lt;T&gt; type);
}</code></pre>
                    </div>

                    <div class="highlight bg-gray-50 p-6 rounded-lg my-6">
                        <p class="font-medium mb-2 flex items-center"><i class="fas fa-info-circle mr-2 text-indigo-600"></i>关键点</p>
                        <p class="text-gray-700"><code class="bg-gray-200 px-1 py-0.5 rounded">getMapper(Class&lt;T&gt; type)</code>方法通过动态代理技术返回一个Mapper接口的代理对象，代理对象会将接口方法调用转发给<code class="bg-gray-200 px-1 py-0.5 rounded">MapperMethod</code>，最终执行SQL。</p>
                    </div>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public class DefaultSqlSession implements SqlSession {
    @Override
    public &lt;T&gt; T getMapper(Class&lt;T&gt; type) {
        // 创建代理对象，将接口方法调用转发到对应的Mapper方法
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class&lt;?&gt;[]{type}, new MapperProxy(this));
    }
}</code></pre>
                    </div>

                    <p class="text-gray-700 mb-6">在<code class="bg-gray-100 px-2 py-1 rounded">DefaultSqlSession</code>的实现中，<code class="bg-gray-100 px-2 py-1 rounded">Proxy.newProxyInstance()</code>方法被用来创建Mapper接口的代理对象，并将调用交由<code class="bg-gray-100 px-2 py-1 rounded">MapperProxy</code>来处理。</p>

                    <h2 class="text-3xl font-bold mb-6 text-gray-800 mt-12">3. MapperProxy的工作原理</h2>
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">MapperProxy</code>是MyBatis中的动态代理实现，负责拦截方法调用并将其转化为SQL执行的操作。<code class="bg-gray-100 px-2 py-1 rounded">MapperProxy</code>实现了<code class="bg-gray-100 px-2 py-1 rounded">InvocationHandler</code>接口，通过重写<code class="bg-gray-100 px-2 py-1 rounded">invoke</code>方法来处理对Mapper接口方法的调用。</p>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public class MapperProxy&lt;T&gt; implements InvocationHandler {
    private final SqlSession sqlSession;
    private final MapperMethod mapperMethod;

    public MapperProxy(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        this.mapperMethod = new MapperMethod();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 在调用Mapper接口方法时，创建MapperMethod并执行
        return mapperMethod.execute(sqlSession, method, args);
    }
}</code></pre>
                    </div>

                    <div class="highlight bg-gray-50 p-6 rounded-lg my-6">
                        <p class="font-medium mb-2 flex items-center"><i class="fas fa-lightbulb mr-2 text-indigo-600"></i>关键机制</p>
                        <p class="text-gray-700"><code class="bg-gray-200 px-1 py-0.5 rounded">MapperProxy</code>通过<code class="bg-gray-200 px-1 py-0.5 rounded">MapperMethod</code>来执行具体的SQL操作。<code class="bg-gray-200 px-1 py-0.5 rounded">MapperMethod</code>封装了映射器方法和SQL语句的对应关系。</p>
                    </div>

                    <h2 class="text-3xl font-bold mb-6 text-gray-800 mt-12">4. MapperMethod的作用与执行流程</h2>
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">MapperMethod</code>是MyBatis的一个核心类，负责将Mapper接口方法和SQL语句进行绑定，并通过<code class="bg-gray-100 px-2 py-1 rounded">SqlSession</code>执行对应的SQL操作。</p>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public class MapperMethod {
    public Object execute(SqlSession sqlSession, Method method, Object[] args) throws Throwable {
        // 获取映射的SQL语句及其参数类型
        MappedStatement mappedStatement = sqlSession.getConfiguration().getMappedStatement(method.getName());
        BoundSql boundSql = mappedStatement.getBoundSql(args);
        // 执行SQL
        return sqlSession.selectOne(boundSql.getSql(), args);
    }
}</code></pre>
                    </div>

                    <ul class="list-disc pl-6 space-y-2 text-gray-700 mb-6">
                        <li><code class="bg-gray-100 px-1 py-0.5 rounded">execute()</code>方法首先根据方法名获取对应的<code class="bg-gray-100 px-1 py-0.5 rounded">MappedStatement</code></li>
                        <li>然后通过<code class="bg-gray-100 px-1 py-0.5 rounded">MappedStatement</code>构建<code class="bg-gray-100 px-1 py-0.5 rounded">BoundSql</code>对象</li>
                        <li>最后执行SQL语句</li>
                    </ul>

                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>包含了SQL语句和执行方式（如查询、插入、更新等），并通过<code class="bg-gray-100 px-2 py-1 rounded">BoundSql</code>与具体的参数绑定。</p>

                    <h2 class="text-3xl font-bold mb-6 text-gray-800 mt-12">5. MappedStatement的构建与SQL绑定</h2>
                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>是MyBatis中的核心对象之一，封装了一个SQL语句及其执行的相关信息。每个<code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>对应一个<code class="bg-gray-100 px-2 py-1 rounded">Mapper</code>中的SQL语句，MyBatis通过<code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>来查找和执行SQL语句。</p>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public class MappedStatement {
    private final String id;
    private final SqlCommandType sqlCommandType;
    private final BoundSql boundSql;

    public MappedStatement(String id, SqlCommandType sqlCommandType, BoundSql boundSql) {
        this.id = id;
        this.sqlCommandType = sqlCommandType;
        this.boundSql = boundSql;
    }

    public BoundSql getBoundSql(Object parameter) {
        // 将参数与SQL语句绑定
        return boundSql;
    }
}</code></pre>
                    </div>

                    <p class="text-gray-700 mb-6"><code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>通过<code class="bg-gray-100 px-2 py-1 rounded">SqlCommandType</code>标识SQL的类型（查询、插入、更新等），并通过<code class="bg-gray-100 px-2 py-1 rounded">BoundSql</code>将SQL语句和参数进行绑定。</p>

                    <h2 class="text-3xl font-bold mb-6 text-gray-800 mt-12">6. 动态代理与SQL映射的执行流程</h2>
                    <p class="text-gray-700 mb-6">动态代理的执行流程从<code class="bg-gray-100 px-2 py-1 rounded">SqlSession.getMapper()</code>开始，最终通过<code class="bg-gray-100 px-2 py-1 rounded">MapperProxy</code>、<code class="bg-gray-100 px-2 py-1 rounded">MapperMethod</code>和<code class="bg-gray-100 px-2 py-1 rounded">MappedStatement</code>执行SQL。以下是具体的执行流程：</p>

                    <div class="bg-blue-50 p-6 rounded-lg mb-6">
                        <ol class="list-decimal pl-6 space-y-3 text-gray-700">
                            <li><strong>获取Mapper接口代理对象</strong>：当调用<code class="bg-blue-100 px-1 py-0.5 rounded">SqlSession.getMapper()</code>时，MyBatis使用<code class="bg-blue-100 px-1 py-0.5 rounded">Proxy.newProxyInstance()</code>创建一个Mapper接口的动态代理对象，并将调用交给<code class="bg-blue-100 px-1 py-0.5 rounded">MapperProxy</code>处理。</li>
                            <li><strong>调用Mapper方法</strong>：当通过代理对象调用Mapper接口的方法时，<code class="bg-blue-100 px-1 py-0.5 rounded">MapperProxy</code>的<code class="bg-blue-100 px-1 py-0.5 rounded">invoke()</code>方法被触发，<code class="bg-blue-100 px-1 py-0.5 rounded">invoke()</code>方法通过<code class="bg-blue-100 px-1 py-0.5 rounded">MapperMethod.execute()</code>来执行对应的SQL操作。</li>
                            <li><strong>执行SQL</strong>：在<code class="bg-blue-100 px-1 py-0.5 rounded">MapperMethod.execute()</code>方法中，<code class="bg-blue-100 px-1 py-0.5 rounded">MappedStatement</code>根据方法名查找对应的SQL语句，并使用<code class="bg-blue-100 px-1 py-0.5 rounded">BoundSql</code>将参数与SQL进行绑定，最终通过<code class="bg-blue-100 px-1 py-0.5 rounded">SqlSession.selectOne()</code>执行SQL并返回结果。</li>
                            <li><strong>返回结果</strong>：<code class="bg-blue-100 px-1 py-0.5 rounded">MapperMethod.execute()</code>最终调用<code class="bg-blue-100 px-1 py-0.5 rounded">sqlSession.selectOne()</code>执行SQL并返回查询结果。执行结果通过<code class="bg-blue-100 px-1 py-0.5 rounded">selectOne()</code>方法传递给调用者。</li>
                        </ol>
                    </div>

                    <div class="code-block my-6">
                        <pre><code class="language-java">public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // 通过MapperMethod执行SQL
    return mapperMethod.execute(sqlSession, method, args);
}</code></pre>
                    </div>
                </article>
            </div>

            <!-- Sidebar -->
            <div class="lg:col-span-1">
                <div class="sticky top-6 space-y-6">
                    <!-- Key Concepts Card -->
                    <div class="card bg-white p-6 rounded-lg shadow-sm">
                        <h3 class="text-xl font-bold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-key mr-2 text-indigo-600"></i>核心概念
                        </h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-xs"></i>
                                <span>动态代理将接口方法映射到SQL</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-xs"></i>
                                <span>SqlSession是执行SQL的入口</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-xs"></i>
                                <span>MapperProxy处理方法调用</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-xs"></i>
                                <span>MapperMethod执行SQL操作</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2 text-xs"></i>
                                <span>MappedStatement封装SQL信息</span>
                            </li>
                        </ul>
                    </div>

                    <!-- Flowchart Section -->
                    <div id="flowchart" class="card bg-white p-6 rounded-lg shadow-sm">
                        <h3 class="text-xl font-bold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-project-diagram mr-2 text-indigo-600"></i>执行流程图
                        </h3>
                        <div class="mermaid">
                            graph TD
                                A[调用SqlSession.getMapper()] --> B[创建Mapper代理对象]
                                B --> C[调用Mapper接口方法]
                                C --> D[MapperProxy.invoke()]
                                D --> E[MapperMethod.execute()]
                                E --> F[获取MappedStatement]
                                F --> G[创建BoundSql]
                                G --> H[执行SQL语句]
                                H --> I[返回结果]
                        </div>
                    </div>

                    <!-- Related Classes Card -->
                    <div class="card bg-white p-6 rounded-lg shadow-sm">
                        <h3 class="text-xl font-bold mb-4 text-gray-800 flex items-center">
                            <i class="fas fa-cubes mr-2 text-indigo-600"></i>相关类
                        </h3>
                        <div class="space-y-4">
                            <div>
                                <h4 class="font-medium text-gray-700 mb-2">SqlSession</h4>
                                <p class="text-sm text-gray-600">MyBatis操作数据库的主要接口，提供获取Mapper和直接执行SQL的方法。</p>
                            </div>
                            <div>
                                <h4 class="font-medium text-gray-700 mb-2">MapperProxy</h4>
                                <p class="text-sm text-gray-600">实现InvocationHandler接口，拦截Mapper方法调用并转发到MapperMethod。</p>
                            </div>
                            <div>
                                <h4 class="font-medium text-gray-700 mb-2">MapperMethod</h4>
                                <p class="text-sm text-gray-600">将Mapper方法调用转换为具体的SQL执行操作。</p>
                            </div>
                            <div>
                                <h4 class="font-medium text-gray-700 mb-2">MappedStatement</h4>
                                <p class="text-sm text-gray-600">封装SQL语句、参数映射和结果映射等信息。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center">
                <h3 class="text-lg font-medium mb-2">技术小馆</h3>
                <p class="text-sm">
                    <a href="http://www.yuque.com/jtostring" class="hover:text-white transition">http://www.yuque.com/jtostring</a>
                </p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```